package com.xjm.controller;

import com.xjm.controller.frontend.MainPageController;
import com.xjm.controller.superadmin.HeadLineOperationController;
import com.xjm.enums.HttpMethod;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author jaymin<br>
 * 模拟SpringMVC架构编写一个DispatcheServlet<br>
 * <h4>@WebServlet设置为"/",即可拦截几乎所有的请求,不拦截jsp的请求</h4>
 * <h4>切勿设置为"/*",这会让转发到jsp这个请求重新进入"/*"的处理接口，进而无限自循环调用</h4>
 * <h4>详情可以查看tomcat目录的conf/web.xml</h4>
 * <h4>@See: The mappings for the JSP servlet</h4>
 * 2020/11/7 22:13
 */
@Slf4j
@WebServlet("/")
public class DispatcherServlet extends HttpServlet {
    @Override
    public void init(){
        System.out.println("我是首次请求被处理前执行的，后续不会再执行");
    }
    /**
     * 统一拦截所有HTTP请求，对路由进行匹配不同的Controller方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        String method = req.getMethod();
        log.info("---DispatcherServlet is resolving the HTTP request. \nthe request path is : {} \n" +
                "the request method is :{} \n", servletPath, method);
        if (Objects.equals(servletPath, "/frontend/mainpage/info") && HttpMethod.GET.name().equals(method)) {
            new MainPageController().getMainPageInfo(req, resp);
        }
    }
}
